'''
Created on Oct 21, 2011

@author: onetera
'''


from xml.dom import minidom, Node
import os


PROJECT_SERVER = "C:\APM_Setup\htdocs\\"
folderStructureXML = r"D:\kali\General\papyrus\lib\folderStructure.xml"

class papyfolder:
    '''
    To create folder tree through xml file.
    '''
    def __init__(self , folderStructureXML = folderStructureXML ):
        '''
        Create folder Structure instance.
        '''
        f = open( folderStructureXML )
        self.xmldoc = minidom.parse( f )
        f.close()        
        self.cursor = ''
        self.prjName = ''
        
    def createLeaf(self , node):             
        if node == None:
            return
        if  node.nodeType == 1 :                       
            self.cursor += '\\'+node.nodeName
            thePath =  self.cursor 
#            thePath =  PROJECT_SERVER + self.prjName +'\\' + self.cursor
#            thePath =  PROJECT_SERVER + self.prjName +'\\'+ self.detachStrt( self.cursor )
            if not  '__' in thePath:   
                print thePath                          
                self.makedir( thePath )
        if node.childNodes != [] :
            self.createLeaf( node.firstChild )                              
        if node.nextSibling :
            if node.nodeType == 1 :  
                self.cursor = self.detachEnd( self.cursor )                       
            self.createLeaf( node.nextSibling )
        else :
            return        
    
    def detachEnd(self , string ):
        temp = string.split('\\')
        return '\\'.join(temp[:-1])
    
    def detachStrt(self , string ):
        temp = string.split('\\')
        return '\\'.join(temp[2:])         
        
    def makedir(self , thePath ):
        if not os.path.isdir(thePath):
            os.makedirs( thePath )
        else :
            return

    def setElemName(self , elem , newElem ):
        tg = self.xmldoc.getElementsByTagName( elem )
        tg[0].nodeName = newElem
        return tg        
        
    def createProj( self , prjName ):
        prjElem = self.setElemName('__Project' , prjName ) 
        self.cursor =  PROJECT_SERVER + self.prjName                     
        self.makedir( self.cursor )
        self.createLeaf( self.xmldoc.childNodes[0] )
        
    def createSeq(self , prjName , seqName ):
        prjElem = self.setElemName('__Project' , prjName )
        seqElem = self.setElemName('__Sequence' , seqName)        
        self.cursor = PROJECT_SERVER + prjName + '\\VFX\\'
        self.createLeaf( seqElem[0] )
        
    def createShot(self , prjName , seqName , shotName ):
        prjElem = self.setElemName('__Project' , prjName )
        seqElem = self.setElemName('__Sequence' , seqName)
        shotElem = self.setElemName('__Shot' , shotName)        
        self.cursor = PROJECT_SERVER + prjName + '\\VFX\\'
        self.createLeaf( seqElem[0] )        

if __name__ == '__main__':
    papyfolder = papyfolder()
    import sys    
    projectName = sys.argv[1] 
    Sequences = ['APM' , 'ROD' , 'OPA']
    Shot = ['301' , '318' , '354' , '234']       
    papyfolder.createProj( projectName )
    map( lambda x : papyfolder.createSeq( projectName , x ) , Sequences )
    map( lambda x : papyfolder.createShot( projectName , Sequences[0] , x ) , Shot )
        
            